package dao.impl;

import bean.Movie;
import dao.MovieDao;
import util.DBCon;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MovieDaoImpl implements MovieDao {
    @Override
    public List<Movie> getAllMovie() {
        Connection conn = DBCon.getConnection();
        if(conn == null){
            System.out.println("数据库连接失败");
        }
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from table_movie");
            List<Movie> movieList = new ArrayList<>();
            while (rs.next()){
                Movie movie = new Movie();
                movie.setId(rs.getInt(1));
                movie.setName(rs.getString(2));
                movie.setTotalTime(rs.getInt(3));
                movie.setMain_actor(rs.getString(4));
                movie.setImg(rs.getString(5));
                movie.setVideo(rs.getString(6));
                movie.setPrice(rs.getBigDecimal(7));
                movie.setType(rs.getString(8));
                movie.setdetail(rs.getString(9));
                movie.setDate(rs.getString(10));
                movieList.add(movie);
            }
            return movieList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Movie getMovieById(int movieId) {
        Connection conn = DBCon.getConnection();
        try {
            PreparedStatement pStmt = conn.prepareStatement("select * from table_movie where m_id = ?");
            pStmt.setInt(1,movieId);
            ResultSet rs = pStmt.executeQuery();

            if(rs.next()){
                Movie movie = new Movie();
                movie.setId(rs.getInt(1));
                movie.setName(rs.getString(2));
                movie.setTotalTime(rs.getInt(3));
                movie.setMain_actor(rs.getString(4));
                movie.setImg(rs.getString(5));
                movie.setVideo(rs.getString(6));
                movie.setPrice(rs.getBigDecimal(7));
                movie.setType(rs.getString(8));
                movie.setdetail(rs.getString(9));
                movie.setDate(rs.getString(10));
                return movie;
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Movie> getMoviesByKey(String key) {
        Connection conn = DBCon.getConnection();
        if(conn == null){
            System.out.println("数据库连接失败");
        }
        try {
            PreparedStatement pStmt = conn.prepareStatement("select * from table_movie where name like ?");
            String key1 = "%" + key + "%";
            pStmt.setString(1,key1);
            ResultSet rs = pStmt.executeQuery();
            List<Movie> movieList = new ArrayList<>();
            while (rs.next()){
                Movie movie = new Movie();
                movie.setId(rs.getInt(1));
                movie.setName(rs.getString(2));
                movie.setTotalTime(rs.getInt(3));
                movie.setMain_actor(rs.getString(4));
                movie.setImg(rs.getString(5));
                movie.setVideo(rs.getString(6));
                movie.setPrice(rs.getBigDecimal(7));
                movie.setType(rs.getString(8));
                movie.setdetail(rs.getString(9));
                movie.setDate(rs.getString(10));
                movieList.add(movie);
            }
            return movieList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public boolean deleteMovie(int movieId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBCon.getConnection();
            preparedStatement = connection.prepareStatement("DELETE FROM table_movie WHERE m_id = ?");
            preparedStatement.setInt(1, movieId);
            int deleteNum = preparedStatement.executeUpdate();
            if(deleteNum>0){
                System.out.println("删除成功");
                return true;
            }
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean modifyMovie(Movie movie) {
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库连接失败");
            return false;
        }
        try {
            PreparedStatement preparedStatement = con.prepareStatement("update table_movie set name = ?,type = ?,main_actor=?," +
                    "date = ?,total_time = ?,price = ?  where m_id = ?");
            preparedStatement.setString(1,movie.getName());
            preparedStatement.setString(2,movie.getType());

            preparedStatement.setString(3,movie.getMain_actor());
            preparedStatement.setString(4,movie.getDate());
            preparedStatement.setLong(5,movie.getTotalTime());
            preparedStatement.setBigDecimal(6,movie.getPrice());
            preparedStatement.setInt(7,movie.getId());
           int updateNum = preparedStatement.executeUpdate();
           if(updateNum > 0){
               return true;
           }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
    public boolean addMovie(Movie movie) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        boolean success = false;
        try {
            connection = DBCon.getConnection();
            preparedStatement = connection.prepareStatement("INSERT INTO table_movie (name,total_time,main_actor,img,video,price,type,detail,date ) VALUES (?, ?, ?, ?, ?, ?, ?, ?,?)");
            preparedStatement.setString(1, movie.getName());
            preparedStatement.setLong(2, movie.getTotalTime());
            preparedStatement.setString(3,movie.getMain_actor());
            preparedStatement.setString(4,movie.getImg() );
            preparedStatement.setString(5,movie.getVideo());
            preparedStatement.setBigDecimal(6,movie.getPrice());
            preparedStatement.setString(7,movie.getType());
            preparedStatement.setString(8,movie.getdetail());
            preparedStatement.setString(9,movie.getDate());
            preparedStatement.executeUpdate();
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return success;
    }
}
